package com.javalab._6feature._7io;

import java.io.File;
import java.io.IOException;

public class _2File {
    public static void main(String[] args) {
        // 创建目录
        File folder = new File("D:/myfolder");
        boolean mkdir = folder.mkdir();
        if (mkdir) {
            System.out.println("创建目录成功");
        } else {
            System.out.println("目录已经存在");
        }

        // 创建文件
        File file = new File("D:/myfolder/1.txt");
        try {
            boolean newFile = file.createNewFile();
            if (newFile) {
                System.out.println("创建文件成功");
            } else {
                System.out.println("文件已经存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 查看目录下的内容
        String[] list = folder.list();
        for (String s : list) {
            System.out.println(s);
        }
        // 打印目录名
        String folderName = folder.getName();
        System.out.println("目录名:" + folderName);
        // 打印文件绝对路径
        String absolutePath = file.getAbsolutePath();
        System.out.println("文件绝对路径:" + absolutePath);

        // 删除文件
        boolean delete = file.delete();
        if (delete) {
            System.out.println("删除文件成功");
        } else {
            System.out.println("删除文件失败");
        }
    }
}
